package com.sprinboot_vue.blog_springboot.controller;

import com.sprinboot_vue.blog_springboot.entity.Blog_message;
import com.sprinboot_vue.blog_springboot.repository.Blog_messageRepository;
import com.sprinboot_vue.blog_springboot.result.Result;
import com.sprinboot_vue.blog_springboot.result.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Thomas
 * @version 1.0
 * @date 2021/12/5 10:21
 */
@RestController
@RequestMapping("/message")
@Api(tags = "留言的接口")
public class Blog_messageHandler {
    @Autowired
    private Blog_messageRepository blog_messageRepository;

    //保存留言
    @PostMapping("/save_message/{message}")
    @ApiOperation("保存留言")
    public Result save_message(@PathVariable("message") String message){
        try{
            Blog_message blog_message=new Blog_message();
            blog_message.setMessage(message);
            Date date=new Date();
            String time_ = BlogGetDate_message(date);
            blog_message.setTime_(time_);
            blog_messageRepository.save(blog_message);
            return Result.success().codeAndmessage(ResultInfo.SUCCESS);
        }catch (Exception e){
            return Result.error().codeAndmessage(ResultInfo.ERROR);
        }
    }
    //获得当前的时间  2021.12.23 12:23:59
    private String BlogGetDate_message(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(date);
    }

    //保存留言
    @GetMapping("/findAllMessage")
    @ApiOperation("返回所有的留言")
    public Result findAllMessage(){
        List<Blog_message> all = blog_messageRepository.findAll();
        return Result.success().codeAndmessage(ResultInfo.SUCCESS).date("data",all);
    }
}
